package com.ltmonitor.web.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ltmonitor.web.util.JsonMessage;

import javassist.expr.NewArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/platformFun")
public class PlatformFunController extends PaginateAction {

	/**
	 * 请求809状态监控页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/viewJt809StatusDataPage.action")
	public String viewJt809StatusDataPage(Model model){
		model.addAttribute("message", "viewJt809StatusPage");
		return "platform/jt809StatusDataPage";
	}
	
	
	/**
	 * 请求车辆定位数据转发设置页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/viewJt809TransPage.action")
	public String viewJt809TransPage(Model model,
			@RequestParam(value="name" , defaultValue="" )String name,
			@RequestParam(value="startTime" , defaultValue="" )String startTime,
			@RequestParam(value="endTime" , defaultValue="" )String endTime
			){
		Map<String, Object> map = new HashedMap();
		map.put("name", name);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		putSession("transParam", JSONObject.fromObject(map));
		return "platform/jt809TransPage";
	}
	
	
	/**
	 * 请求模拟GPS定位数据页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/viewImitateGpsDataPage.action")
	public String viewImitateGpsDataPage(Model model){
		return "platform/imitateGpsDataPage";
	}
	
	
	private String queryIdTransIn = "platform.insertOneVehDataForTransData";
	/**
	 * 插入选中的数据到转发表格
	 * @param jsonStr
	 * @return
	 */
	@RequestMapping("/insertSlctedVehForTrans.action")
	@ResponseBody
	public JsonMessage insertSlctedVehForTrans(@RequestBody String jsonStr){
		JsonMessage result = null;
		String msg = "";
		try {
			Map map = JSONObject.fromObject(jsonStr);
			if(map.containsKey("rows")){
				List rows = (List) map.get("rows");
//				System.out.println("需要添加的车辆数据："+ rows.toString());
				if(rows.size() > 0){
					for(int i=0 ; i<rows.size() ; i++){
						Map temp = (Map) rows.get(i);
						temp.put("createDate", map.get("createDate").toString());
						temp.put("status", 0);
						temp.put("remark", "");
//						System.out.println("需要添加的车辆数据："+ temp.toString());
						//开始添加
						queryService.insert(queryIdTransIn,temp);
						if(!temp.containsKey("id")){
							msg += "添加车辆失败：" + temp.toString();
						}
					}
				}
			}else {
				msg += "请求的参数中不包含车辆数据，无法插入数据！";
			}
			if(msg.equals("")){
				result = new JsonMessage(true, null);
			}else {
				result = new JsonMessage(false, msg);
			}
		} catch (Exception e) {
			// TODO: handle exception
			msg += "插入选中的车辆失败：" + e.getMessage();
			result = new JsonMessage(false, msg);
		}
		
		return result;
	}
}
